package com.lening.common.config;

import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.RedisSerializer;
//import org.springframework.boot.autoconfigure.data.redis.LettuceConnectionConfiguration;

/**
 * @author 李猛
 * @datetime 2024/4/22 18:25
 * @description 类对象
 */
@Configuration
public class RedisConfiguration {
    @Bean
//    @ConditionalOnClass(LettuceConnectionConfiguration.class)
    public LettuceConnectionFactory redisConnectionFactory() {
        return new LettuceConnectionFactory();
    }

    @Bean
    public RedisTemplate<String, String> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
        RedisTemplate<String, String> template = new RedisTemplate<>();

        /**
         * 序列化
         */
        template.setKeySerializer(RedisSerializer.string());
        template.setValueSerializer(RedisSerializer.string());
        template.setHashKeySerializer(RedisSerializer.string());
        template.setHashValueSerializer(RedisSerializer.string());

        template.setConnectionFactory(redisConnectionFactory);
        return template;
    }
}
